<?php
include('../data/comm.inc.php');
include('../data/myadminvar.php');
include('../func/func.php');
include('../func/csfunc.php');
include('../func/adminfunc.php');
include('../include.php');
include('./checklogin.php');
switch ($_REQUEST['xtype']) {
    case "show":
        $sdate = week();
        $tpl->assign("sdate", $sdate);
        $gamecs = getgamecs($userid);
        $gamecs = getgamename($gamecs);
        $fl     = array();
        foreach ($gamecs as $key => $val) {
            if (!in_array($val['flname'], $fl) & $val['gid'] != 107 & $val['gid'] != 100) {
                $fl[$val['fenlei']] = $val['flname'];
            }
        }
        
        $tpl->assign("gamecs", $gamecs);
        $tpl->assign("fl", $fl);
        $tpl->assign("topid", $userid);
        $tpl->assign("gid", $gid);
        $tpl->assign("layer", transuser($userid, 'layer'));
        $tpl->assign("username", transu($userid));
        $tpl->assign("layername", $config['layer']);
        $tpl->display("baox.html");
        break;
    case "bao":
	    $jsstatus   = $_POST['jsstatus'];
        $bid   = $_POST['bid'];
        $cid   = $_POST['cid'];
        $sid   = $_POST['sid'];
        $start = rdates($_POST['start']);
        $end   = rdates($_POST['end']);
        $uid   = $_POST['uid'];
        $game  = $_POST['game'];
        $game  = explode('|', $game);
        array_pop($game);


        
        $gstr  = '(' . implode(',', $game) . ')';
        //$start = strtotime($start . ' ' . $config['editend']);
        //$end   = strtotime($end . ' ' . $config['editstart']) + 86400;
		
		//$start =sqltime($start);
		//$end =sqltime($end);
        $whi   = "  and dates>='$start' and dates<='$end' ";
        $yq    = " and xtype!=2 $whi and bs=1";
        $yq2   = " and xtype=2  $whi and bs=1";
        if (is_numeric($bid)) {
            $yq .= " and bid='$bid' ";
            $yq2 .= " and bid='$bid' ";
        }
        if (is_numeric($sid)) {
            $yq .= " and sid='$sid' ";
            $yq2 .= " and sid='$sid' ";
        }
        if (is_numeric($cid)) {
            $yq .= " and cid='$cid' ";
            $yq2 .= " and cid='$cid' ";
        }
        $cg    = count($game);
        $bao   = topuser($userid);
        $cb    = count($bao);
        $zbao  = array();
        $start = microtime();
        for ($j = 0; $j < $cg; $j++) {
            $join               = " from `$tb_lib` where  gid='" . $game[$j] . "' ";
			$jstr='';
			if($jsstatus==1){
				$jstr .= " and z!=9 ";
			}else if($jsstatus==0){
				$jstr .= " and z=9 ";
			}
            $ustr               = 'uid1';
            $gbao[$j][0]['gid'] = $game[$j];
            $msql->query("select gname,class from `$tb_game` where gid='{$game[$j]}'");
            $msql->next_record();
            $gbao[$j][0]['gname'] = $msql->f('gname');
            $gbao[$j][0]['style'] = $msql->f('class');
            for ($i = 0; $i < $cb; $i++) {
                $whi2 = " and ($ustr='" . $bao[$i]['userid'] . "' or userid='" . $bao[$i]['userid'] . "') $yq $jstr";
                $sql  = "select sum(je),count(id),sum(points*je/100) $join $whi2";
                $msql->query($sql);
                $msql->next_record();
                $gbao[$j][$i]['userzs']     = pr0($msql->f(1));
                $gbao[$j][$i]['userje']     = pr2($msql->f(0));
                $gbao[$j][$i]['userpoints'] = pr2($msql->f(2));
                if ($bao[$j][$i]['userzs'] == 0) {
                }
                $sql = "select sum(je*zc0/100),count(id),sum(if($ustr=0,points,points1)*je*zc0/(100*100)) $join and z!=2 and z!=7   $whi2 ";
                $msql->query($sql);
                $msql->next_record();
                $gbao[$j][$i]['zs']     = pr0($msql->f(1));
                $gbao[$j][$i]['upje']   = pr2($msql->f(0));
                $gbao[$j][$i]['points'] = pr2($msql->f(2));
                
                $sql = "select sum(if($ustr=0,peilv1,peilv11)*je*zc0/(100)) $join and z=1 $whi2 ";
                $msql->query($sql);
                $msql->next_record();
                $gbao[$j][$i]['zhong'] = pr2($msql->f(0));
                $sql                   = "select sum(if($ustr=0,peilv2,peilv21)*je*zc0/(100)) $join and z=3 $whi2  ";
                
                $msql->query($sql);
                $msql->next_record();
                $gbao[$j][$i]['zhong'] += pr2($msql->f(0));
                if ($game[$j] == 100) {
                    $sql = "select sum(prize*zc0/100) $join and z=5 $whi2";
                    $msql->query($sql);
                    $msql->next_record();
                    $gbao[$j][$i]['zhong'] += pr2($msql->f(0));
                }
                $gbao[$j][$i]['yk']       = pr2($gbao[$j][$i]['upje'] - $gbao[$j][$i]['points'] - $gbao[$j][$i]['zhong']);
                $gbao[$j][$i]['userid']   = $bao[$i]['userid'];
                $gbao[$j][$i]['username'] = $bao[$i]['username'];
                $gbao[$j][$i]['ifagent']  = $bao[$i]['ifagent'];
                $gbao[$j][$i]['layer']    = $bao[$i]['layer'];
                $zbao[$i]['userzs'] += $gbao[$j][$i]['userzs'];
                $zbao[$i]['userje'] += $gbao[$j][$i]['userje'];
                $zbao[$i]['userpoints'] += $gbao[$j][$i]['userpoints'];
                $zbao[$i]['zs'] += $gbao[$j][$i]['zs'];
                $zbao[$i]['upje'] += $gbao[$j][$i]['upje'];
                $zbao[$i]['points'] += $gbao[$j][$i]['points'];
                $zbao[$i]['zhong'] += $gbao[$j][$i]['zhong'];
                $zbao[$i]['yk'] += $gbao[$j][$i]['yk'];
                $zbao[$i]['userid']   = $bao[$i]['userid'];
                $zbao[$i]['username'] = $bao[$i]['username'];
                $zbao[$i]['ifagent']  = $bao[$i]['ifagent'];
                $zbao[$i]['layer']    = $bao[$i]['layer'];
            }
            
            $gbao[$j][$i]['username'] = "fly2";
            $gbao[$j][$i]['fly']      = 2;
            $gbao[$j][$i]['ifagent']  = 0;
            $gbao[$j][$i]['userid']   = $userid;
            $gbao[$j][$i]['username'] = "集团外补";
            $msql->query("select sum(je),count(id),sum(je*points/100) $join  and userid='$userid' and z!=2 and z!=7  $yq2 ");
            $msql->next_record();
            $gbao[$j][$i]['zs']     = pr0($msql->f(1));
            $gbao[$j][$i]['upje']   = pr2($msql->f(0));
            $gbao[$j][$i]['points'] = pr2($msql->f(2));
            $msql->query("select sum(peilv1*je)  $join  and userid='$userid' and z=1 $yq2 ");
            $msql->next_record();
            $gbao[$j][$i]['zhong'] = pr2($msql->f(0));
            $msql->query("select sum(peilv2*je)  $join  and userid='$userid' and z=3 $yq2 ");
            $msql->next_record();
            $gbao[$j][$i]['zhong'] += pr2($msql->f(0));
            
            $gbao[$j][$i]['yk']         = pr2($gbao[$j][$i]['points'] + $gbao[$j][$i]['zhong'] - $gbao[$j][$i]['upje']);
            $gbao[$j][$i]['upje']       = 0 - $gbao[$j][$i]['upje'];
            $gbao[$j][$i]['userje']     = 0;
            $gbao[$j][$i]['userzs']     = $gbao[$j][$i]['zs'];
            $gbao[$j][$i]['userpoints'] = 0;
            $zbao[$i]['username']       = "fly2";
            $zbao[$i]['zs'] += $gbao[$j][$i]['zs'];
            $zbao[$i]['upje'] += $gbao[$j][$i]['upje'];
            $zbao[$i]['points'] += $gbao[$j][$i]['points'];
            $zbao[$i]['zhong'] += $gbao[$j][$i]['zhong'];
            $zbao[$i]['yk'] += $gbao[$j][$i]['yk'];
            $zbao[$i]['userje']     = 0;
            $zbao[$i]['userzs']     = $zbao[$i]['zs'];
            $zbao[$i]['userpoints'] = 0;
            $zbao[$i]['fly']        = 2;
            $zbao[$i]['ifagent']    = 0;
            $zbao[$i]['userid']     = $userid;
            $zbao[$i]['username']   = $gbao[$j][$i]['username'];
        }
        $end = microtime();
        sort($bao);
        $bao = array(
            "gbao" => $gbao,
            "zbao" => $zbao
        );
        echo json_encode($bao);
        unset($bao);
        unset($zbao);
        break;
    case "agent":
	    $jsstatus   = $_POST['jsstatus'];
        $bid   = $_POST['bid'];
        $cid   = $_POST['cid'];
        $sid   = $_POST['sid'];
        $start = rdates($_POST['start']);
        $end   = rdates($_POST['end']);
        $uid   = $_POST['uid'];
        $game  = $_POST['game'];
        $game  = explode('|', $game);
        array_pop($game);
        $gstr  = '(' . implode(',', $game) . ')';
        //$start = strtotime($start . ' ' . $config['editend']);
        //$end   = strtotime($end . ' ' . $config['editstart']) + 86400;
		//$start =sqltime($start);
		//$end =sqltime($end);
        $whi   = "  and dates>='$start' and dates<='$end' ";
        $yq    = " and xtype!=2 $whi and bs=1";
        $yq2   = " and xtype=2  $whi and bs=1";
        if (is_numeric($bid)) {
            $yq .= " and bid='$bid' ";
            $yq2 .= " and bid='$bid' ";
        }
        if (is_numeric($sid)) {
            $yq .= " and sid='$sid' ";
            $yq2 .= " and sid='$sid' ";
        }
        if (is_numeric($cid)) {
            $yq .= " and cid='$cid' ";
            $yq2 .= " and cid='$cid' ";
        }
        
        $bao = topuser($uid);
        $cb  = count($bao);
        if ($cb == 0) {
            //$bao = array();
            //echo  json_encode($bao);
            //exit;
        }
        $cg          = count($game);
        $zbao        = array();
		$msql->query("select layer,plc from `$tb_user` where userid='$uid'");
		$msql->next_record();
		$layer       = $msql->f('layer');
		$plc       = $msql->f('plc');
        $myid        = 'uid' . $layer;
        $myzcstr     = 'zc' . $layer;
        $mypointsstr = 'points' . $layer;
        $mypeilv1str = 'peilv1' . $layer;
        $mypeilv2str = 'peilv2' . $layer;
        $start       = microtime();
        for ($j = 0; $j < $cg; $j++) {
            $join = " from `$tb_lib`  where  gid='" . $game[$j] . "'   ";
			
			$jstr='';
			if($jsstatus==1){
				
				$jstr .= " and z!=9 ";
			}else if($jsstatus==0){
				$jstr .= " and z=9 ";
			}
			
            $msql->query("select gname,class from `$tb_game` where gid='{$game[$j]}'");
            $msql->next_record();
            $gbao[$j][0]['gname'] = $msql->f('gname');
            $gbao[$j][0]['style'] = $msql->f('class');
            for ($i = 0; $i < $cb; $i++) {
                $gbao[$j][$i]['userid']   = $bao[$i]['userid'];
                $gbao[$j][$i]['username'] = $bao[$i]['username'];
                $gbao[$j][$i]['ifagent']  = $bao[$i]['ifagent'];
                $gbao[$j][$i]['layer']    = $bao[$i]['layer'];
                $zbao[$i]['userid']       = $bao[$i]['userid'];
                $zbao[$i]['username']     = $bao[$i]['username'];
                $zbao[$i]['ifagent']      = $bao[$i]['ifagent'];
                $zbao[$i]['layer']        = $bao[$i]['layer'];
                if ($layer < 8) {
                    $uidstrdown    = 'uid' . ($layer + 1);
                    $pointsstrdown = 'points' . ($layer + 1);
                    $peilv1strdown = 'peilv1' . ($layer + 1);
                    $peilv2strdown = 'peilv2' . ($layer + 1);
                } else {
                    $uidstrdown    = 'userid';
                    $pointsstrdown = 'points';
                    $peilv1strdown = 'peilv1';
                    $peilv2strdown = 'peilv2';
                }
                $zcstrdown = '';
                for ($k = 8; $k >= $gbao[$j][$i]['layer']; $k--) {
                    $zcstrdown .= '-zc' . $k;
                }
                $zcstrup = $zcstrdown . '-zc' . $k;
                if ($bao[$i]['ifagent'] == '0') {
                    $msql->query("select sum(je),sum(je*points/100),count(id) $join and  userid='" . $bao[$i]['userid'] . "'  and z!=2 and z!=7  $yq $jstr ");
                    $msql->next_record();
                    $gbao[$j][$i]['upje'] = pr2($msql->f(0));
					$gbao[$j][$i]['zje'] = pr2($msql->f(0));
                    $gbao[$j][$i]['shui'] = pr2($msql->f(1));
                    if ($gbao[$j][$i]['upje'] == 0) {
                        $gbao[$j][$i]['zs']        = 0;
                        $gbao[$j][$i]['zhong']     = 0;
                        $gbao[$j][$i]['yk']        = 0;
                        $gbao[$j][$i]['upje']      = 0;
                        $gbao[$j][$i]['shui']      = 0;
                        $gbao[$j][$i]['mezc']      = 0;
                        $gbao[$j][$i]['meshui']    = 0;
                        $gbao[$j][$i]['mezhong']   = 0;
                        $gbao[$j][$i]['meyk']      = 0;
                        $gbao[$j][$i]['sendje']    = 0;
                        $gbao[$j][$i]['sendshui']  = 0;
                        $gbao[$j][$i]['sendzhong'] = 0;
                        $gbao[$j][$i]['sendyk']    = 0;
                        $gbao[$j][$i]['fly']       = 0;
                        $zbao[$i]['zs'] += $gbao[$j][$i]['zs'];
                        $zbao[$i]['upje'] += $gbao[$j][$i]['upje'];
						$zbao[$i]['zje'] += $gbao[$j][$i]['zje'];
                        $zbao[$i]['shui'] += $gbao[$j][$i]['shui'];
                        $zbao[$i]['zhong'] += $gbao[$j][$i]['zhong'];
                        $zbao[$i]['yk'] += $gbao[$j][$i]['yk'];
                        $zbao[$i]['mezc'] += $gbao[$j][$i]['mezc'];
                        $zbao[$i]['meshui'] += $gbao[$j][$i]['meshui'];
                        $zbao[$i]['mezhong'] += $gbao[$j][$i]['mezhong'];
                        $zbao[$i]['meyk'] += $gbao[$j][$i]['meyk'];
                        $zbao[$i]['sendje'] += $gbao[$j][$i]['sendje'];
                        $zbao[$i]['sendshui'] += $gbao[$j][$i]['sendshui'];
                        $zbao[$i]['sendzhong'] += $gbao[$j][$i]['sendzhong'];
                        $zbao[$i]['sendyk'] += $gbao[$j][$i]['sendyk'];
                        $zbao[$i]['fly'] = 0;
                        continue;
                    }
                    $gbao[$j][$i]['zs'] = pr0($msql->f(2));
                    $msql->query("select sum(peilv1*je) $join and  userid='" . $bao[$i]['userid'] . "'  and z=1 $yq ");
                    $msql->next_record();
                    $gbao[$j][$i]['zhong'] = pr2($msql->f(0));
                    $msql->query("select sum(peilv2*je) $join and  userid='" . $bao[$i]['userid'] . "'  and z=3  $yq ");
                    $msql->next_record();
                    $gbao[$j][$i]['zhong'] += pr2($msql->f(0));
                    if ($game[$j] == 100) {
                        $sql = "select sum(prize) $join and userid='" . $bao[$i]['userid'] . "' and  z=5  $yq ";
                        $msql->query($sql);
                        $msql->next_record();
                        $gbao[$j][$i]['zhong'] += pr2($msql->f(0));
                    }
                    $gbao[$j][$i]['yk'] = pr2($gbao[$j][$i]['upje'] - $gbao[$j][$i]['shui'] - $gbao[$j][$i]['zhong']);
                } else {
                    $sql = "select sum((100" . $zcstrdown . ")*je/100),count(id),sum(if($uidstrdown=0,(points*je/100),$pointsstrdown*je*(100 $zcstrdown)/(100*100))),sum(je)  ";
                    $sql .= " $join  and  ($uidstrdown='" . $gbao[$j][$i]['userid'] . "' or userid='" . $gbao[$j][$i]['userid'] . "')  and z!=2 and z!=7   $yq $jstr ";
                    $msql->query($sql);
                    $msql->next_record();
                    $gbao[$j][$i]['zs']   = pr0($msql->f(1));
                    $gbao[$j][$i]['upje'] = pr2($msql->f(0));
					 $gbao[$j][$i]['zje']  = pr2($msql->f(3));
                    if ($gbao[$j][$i]['zs'] == 0) {
                        $gbao[$j][$i]['zs']        = 0;
                        $gbao[$j][$i]['zhong']     = 0;
                        $gbao[$j][$i]['yk']        = 0;
                        $gbao[$j][$i]['upje']      = 0;
                        $gbao[$j][$i]['shui']      = 0;
                        $gbao[$j][$i]['mezc']      = 0;
                        $gbao[$j][$i]['meshui']    = 0;
                        $gbao[$j][$i]['mezhong']   = 0;
                        $gbao[$j][$i]['meyk']      = 0;
                        $gbao[$j][$i]['sendje']    = 0;
                        $gbao[$j][$i]['sendshui']  = 0;
                        $gbao[$j][$i]['sendzhong'] = 0;
                        $gbao[$j][$i]['sendyk']    = 0;
                        $gbao[$j][$i]['fly']       = 0;
                        $zbao[$i]['zs'] += $gbao[$j][$i]['zs'];
                        $zbao[$i]['upje'] += $gbao[$j][$i]['upje'];
						$zbao[$i]['zje'] += $gbao[$j][$i]['zje'];
                        $zbao[$i]['shui'] += $gbao[$j][$i]['shui'];
                        $zbao[$i]['zhong'] += $gbao[$j][$i]['zhong'];
                        $zbao[$i]['yk'] += $gbao[$j][$i]['yk'];
                        $zbao[$i]['mezc'] += $gbao[$j][$i]['mezc'];
                        $zbao[$i]['meshui'] += $gbao[$j][$i]['meshui'];
                        $zbao[$i]['mezhong'] += $gbao[$j][$i]['mezhong'];
                        $zbao[$i]['meyk'] += $gbao[$j][$i]['meyk'];
                        $zbao[$i]['sendje'] += $gbao[$j][$i]['sendje'];
                        $zbao[$i]['sendshui'] += $gbao[$j][$i]['sendshui'];
                        $zbao[$i]['sendzhong'] += $gbao[$j][$i]['sendzhong'];
                        $zbao[$i]['sendyk'] += $gbao[$j][$i]['sendyk'];
                        $zbao[$i]['fly'] = 0;
                        continue;
                    }
                   
                    $gbao[$j][$i]['shui'] = pr2($msql->f(2));
                    $sql                  = "select sum(if($uidstrdown=0,(peilv1*je),$peilv1strdown*(100 $zcstrdown)*je/100)) $join  and ($uidstrdown='" . $bao[$i]['userid'] . "' or userid='" . $gbao[$j][$i]['userid'] . "')  and z=1 $yq $jstr";
                    
                    $msql->query($sql);
                    $msql->next_record();
                    $gbao[$j][$i]['zhong'] = pr2($msql->f(0));
                    $sql                   = "select sum(if($uidstrdown=0,(peilv2*je),$peilv2strdown*(100 $zcstrdown)*je/100)) $join and ($uidstrdown='" . $bao[$i]['userid'] . "' or userid='" . $gbao[$j][$i]['userid'] . "')  and z=3 $yq $jstr";
                    
                    $msql->query($sql);
                    $msql->next_record();
                    $gbao[$j][$i]['zhong'] += pr2($msql->f(0));
                    if ($game[$j] == 100) {
                        $sql = "select sum(prize*(100 $zcstrdown)/100) $join and ($uidstrdown='" . $bao[$i]['userid'] . "' or userid='" . $gbao[$j][$i]['userid'] . "')  and  z=5 $yq $jstr ";
                        $msql->query($sql);
                        $msql->next_record();
                        $gbao[$j][$i]['zhong'] += pr2($msql->f(0));
                    }
                    $gbao[$j][$i]['yk'] = pr2($gbao[$j][$i]['upje'] - $gbao[$j][$i]['shui'] - $gbao[$j][$i]['zhong']);
                    
                }
                $sql = "select sum($myzcstr*je/100),sum(if($uidstrdown=0,(points*$myzcstr*je/(100*100)),$pointsstrdown*$myzcstr*je/(100*100)))  ";
                $sql .= "  $join    and ($uidstrdown='" . $bao[$i]['userid'] . "' or userid='" . $bao[$i]['userid'] . "') and z!=2 and z!=7  $yq $jstr";
                
                $msql->query($sql);
                $msql->next_record();
                $gbao[$j][$i]['mezc']   = pr2($msql->f(0));
                $gbao[$j][$i]['meshui'] = pr2($msql->f(1));
                $sql                    = "select sum(if($uidstrdown=0,(peilv1*$myzcstr)*je/100,$peilv1strdown*$myzcstr*je/100))  ";
                $sql .= "  $join   and ($uidstrdown='" . $bao[$i]['userid'] . "' or userid='" . $bao[$i]['userid'] . "')  and z=1 $yq $jstr ";
                
                $msql->query($sql);
                $msql->next_record();
                $gbao[$j][$i]['mezhong'] = pr2($msql->f(0));
                $sql                     = "select sum(if($uidstrdown=0,(peilv2*$myzcstr)*je/100,$peilv2strdown*$myzcstr*je/100))  ";
                $sql .= "  $join  and ($uidstrdown='" . $bao[$i]['userid'] . "' or userid='" . $bao[$i]['userid'] . "') and z=3 $yq ";
                
                $msql->query($sql);
                $msql->next_record();
                $gbao[$j][$i]['mezhong'] += pr2($msql->f(0));
                if ($game[$j] == 100) {
                    $sql = "select sum(prize*$myzcstr/100) $join   and ($uidstrdown='" . $bao[$i]['userid'] . "' or userid='" . $bao[$i]['userid'] . "') and z=5 $yq $jstr ";
                    $msql->query($sql);
                    $msql->next_record();
                    $gbao[$j][$i]['mezhong'] += pr2($msql->f(0));
                }
                
                $gbao[$j][$i]['meyk'] = pr2($gbao[$j][$i]['mezc'] - $gbao[$j][$i]['meshui'] - $gbao[$j][$i]['mezhong']);
                $sql                  = "select sum((100 $zcstrup)*je/100),sum($mypointsstr*(100 $zcstrup)*je/(100*100))  ";
                $sql .= "  $join  and ($uidstrdown='" . $bao[$i]['userid'] . "' or userid='" . $bao[$i]['userid'] . "' )  and z!=2 and z!=7  $yq $jstr";
                $msql->query($sql);
                $msql->next_record();
                $gbao[$j][$i]['sendje']   = pr2($msql->f(0));
                $gbao[$j][$i]['sendshui'] = pr2($msql->f(1));
                
                
                $sql = "select sum((100 $zcstrup)*je*$mypeilv1str/100) ";
                $sql .= "  $join  and  ($uidstrdown='" . $bao[$i]['userid'] . "' or userid='" . $bao[$i]['userid'] . "' )  and z=1 $yq $jstr";
                
                $msql->query($sql);
                $msql->next_record();
                $gbao[$j][$i]['sendzhong'] = pr2($msql->f(0));
                $sql                       = "select sum((100 $zcstrup)*je*$mypeilv2str/100)  ";
                $sql .= "  $join  and ($uidstrdown='" . $bao[$i]['userid'] . "' or userid='" . $bao[$i]['userid'] . "')  and z=3 $jstr";
                $msql->query($sql);
                $msql->next_record();
                $gbao[$j][$i]['sendzhong'] += pr2($msql->f(0));
                if ($game[$j] == 100) {
                    $sql = "select sum(prize*(100 $zcstrup)/100)  ";
                    $sql .= "  $join and ($uidstrdown='" . $bao[$i]['userid'] . "' or userid='" . $bao[$i]['userid'] . "') and z=5 $yq $jstr";
                    $msql->query($sql);
                    $msql->next_record();
                    $gbao[$j][$i]['sendzhong'] += pr2($msql->f(0));
                }
                
                $gbao[$j][$i]['sendyk'] = pr2($gbao[$j][$i]['sendshui'] + $gbao[$j][$i]['sendzhong'] - $gbao[$j][$i]['sendje']);
                $gbao[$j][$i]['fly']    = 0;
                $zbao[$i]['zs'] += $gbao[$j][$i]['zs'];
                $zbao[$i]['upje'] += $gbao[$j][$i]['upje'];
				$zbao[$i]['zje'] += $gbao[$j][$i]['zje'];
                $zbao[$i]['shui'] += $gbao[$j][$i]['shui'];
                $zbao[$i]['zhong'] += $gbao[$j][$i]['zhong'];
                $zbao[$i]['yk'] += $gbao[$j][$i]['yk'];
                $zbao[$i]['mezc'] += $gbao[$j][$i]['mezc'];
                $zbao[$i]['meshui'] += $gbao[$j][$i]['meshui'];
                $zbao[$i]['mezhong'] += $gbao[$j][$i]['mezhong'];
                $zbao[$i]['meyk'] += $gbao[$j][$i]['meyk'];
                $zbao[$i]['sendje'] += $gbao[$j][$i]['sendje'];
                $zbao[$i]['sendshui'] += $gbao[$j][$i]['sendshui'];
                $zbao[$i]['sendzhong'] += $gbao[$j][$i]['sendzhong'];
                $zbao[$i]['sendyk'] += $gbao[$j][$i]['sendyk'];
                $zbao[$i]['fly'] = 0;
                if (abs($zbao[$i]['meyk']) < 1)
                    $zbao[$i]['meyk'] = 0;
                if (abs($zbao[$i]['sendyk']) < 1)
                    $zbao[$i]['sendyk'] = 0;
                if (abs($zbao[$i]['yk']) < 1)
                    $zbao[$i]['yk'] = 0;
            }
            $gbao[$j][$i]['username'] = "fly1";
            $gbao[$j][$i]['fly']      = 1;
            $gbao[$j][$i]['ifagent']  = 0;
            $gbao[$j][$i]['userid']   = $uid;
		if(transuser($uid,'fudong')==1){
            $gbao[$j][$i]['username'] = transu($uid) . "[自投]";
		}else{
            $gbao[$j][$i]['username'] = transu($uid) . "[内补]";
		}
            $msql->query("select sum(je),count(id),sum(je*points/100) $join and userid='$uid' and z!=2 and z!=7 $yq $jstr ");
            $msql->next_record();
            $gbao[$j][$i]['zs']     = pr0($msql->f(1));
            $gbao[$j][$i]['mezc']   = pr2($msql->f(0));
            $gbao[$j][$i]['meshui'] = pr2($msql->f(2));
            $msql->query("select sum(peilv1*je) $join and  userid='$uid' and z=1 $yq $jstr");
            $msql->next_record();
            $gbao[$j][$i]['mezhong'] = pr2($msql->f(0));
            
            if ($game[$j] == 100) {
                $msql->query("select sum(prize) $join and  userid='$uid' and z=5 $yq $jstr");
                $msql->next_record();
                $gbao[$j][$i]['mezhong'] += pr2($msql->f(0));
            }
            $gbao[$j][$i]['meyk']      = pr2($gbao[$j][$i]['meshui'] + $gbao[$j][$i]['mezhong'] - $gbao[$j][$i]['mezc']);
            $gbao[$j][$i]['sendje']    = $gbao[$j][$i]['mezc'];
            $gbao[$j][$i]['sendshui']  = $gbao[$j][$i]['meshui'];
            $gbao[$j][$i]['sendzhong'] = $gbao[$j][$i]['mezhong'];
            $gbao[$j][$i]['sendyk']    = pr2($gbao[$j][$i]['meshui'] + $gbao[$j][$i]['mezhong'] - $gbao[$j][$i]['mezc']);
            $gbao[$j][$i]['mezc']      = 0 - $gbao[$j][$i]['mezc'];
            $gbao[$j][$i]['meshui']    = 0 - $gbao[$j][$i]['meshui'];
            $gbao[$j][$i]['mezhong']   = 0 - $gbao[$j][$i]['mezhong'];
            $gbao[$j][$i]['upje']      = 0;
			$gbao[$j][$i]['zje']      = 0;
            $gbao[$j][$i]['shui']      = 0;
            $gbao[$j][$i]['zhong']     = 0;
            $gbao[$j][$i]['yk']        = 0;
            $zbao[$i]['zs'] += $gbao[$j][$i]['zs'];
            $zbao[$i]['mezc'] += $gbao[$j][$i]['mezc'];
            $zbao[$i]['meshui'] += $gbao[$j][$i]['meshui'];
            $zbao[$i]['mezhong'] += $gbao[$j][$i]['mezhong'];
            $zbao[$i]['meyk'] += $gbao[$j][$i]['meyk'];
            $zbao[$i]['sendje'] += $gbao[$j][$i]['sendje'];
            $zbao[$i]['sendshui'] += $gbao[$j][$i]['sendshui'];
            $zbao[$i]['sendzhong'] += $gbao[$j][$i]['sendzhong'];
            $zbao[$i]['sendyk'] += $gbao[$j][$i]['sendyk'];
            $zbao[$i]['upje']     = 0;
			$zbao[$i]['zje']     = 0;
            $zbao[$i]['shui']     = 0;
            $zbao[$i]['zhong']    = 0;
            $zbao[$i]['yk']       = 0;
            $zbao[$i]['fly']      = 1;
            $zbao[$i]['ifagent']  = $gbao[$j][$i]['ifagent'];
            $zbao[$i]['userid']   = $uid;
            $zbao[$i]['username'] = $gbao[$j][$i]['username'];
            $i++;
            $gbao[$j][$i]['username'] = "fly1";
            $gbao[$j][$i]['fly']      = 2;
            $gbao[$j][$i]['userid']   = $uid;
            $gbao[$j][$i]['ifagent']  = 0;
            $gbao[$j][$i]['username'] = transu($uid) . "[外补]";
            $join                     = " from `$tb_lib`  where  gid='" . $game[$j] . "'   $yq2 ";
            $msql->query("select sum(je),count(id),sum(je*points/100) $join and  userid='$uid' and z!=2 and z!=7 $yq2 $jstr ");
            $msql->next_record();
            $gbao[$j][$i]['zs']     = pr0($msql->f(1));
            $gbao[$j][$i]['mezc']   = pr2($msql->f(0));
            $gbao[$j][$i]['meshui'] = pr2($msql->f(2));
            $msql->query("select sum(peilv1*je) $join and userid='$uid' and z=1 $yq2");
            $msql->next_record();
            $gbao[$j][$i]['mezhong'] = pr2($msql->f(0));
            $msql->query("select sum(peilv2*je) $join and  userid='$uid' and z=3 $yq2");
            $msql->next_record();
            $gbao[$j][$i]['mezhong'] += pr2($msql->f(0));
            $gbao[$j][$i]['meyk']      = pr2($gbao[$j][$i]['meshui'] + $gbao[$j][$i]['mezhong'] - $gbao[$j][$i]['mezc']);
            $gbao[$j][$i]['sendje']    = 0;
            $gbao[$j][$i]['sendshui']  = 0;
            $gbao[$j][$i]['sendzhong'] = 0;
            $gbao[$j][$i]['mezc']      = 0 - $gbao[$j][$i]['mezc'];
            $gbao[$j][$i]['meshui']    = 0 - $gbao[$j][$i]['meshui'];
            $gbao[$j][$i]['mezhong']   = 0 - $gbao[$j][$i]['mezhong'];
            $gbao[$j][$i]['upje']      = 0;
			$gbao[$j][$i]['zje']      = 0;
            $gbao[$j][$i]['shui']      = 0;
            $gbao[$j][$i]['zhong']     = 0;
            $gbao[$j][$i]['yk']        = 0;
            $gbao[$j][$i]['sendyk']    = 0;
            $zbao[$i]['zs'] += $gbao[$j][$i]['zs'];
            $zbao[$i]['mezc'] += $gbao[$j][$i]['mezc'];
            $zbao[$i]['meshui'] += $gbao[$j][$i]['meshui'];
            $zbao[$i]['mezhong'] += $gbao[$j][$i]['mezhong'];
            $zbao[$i]['meyk'] += $gbao[$j][$i]['meyk'];
            $zbao[$i]['sendje'] += $gbao[$j][$i]['sendje'];
            $zbao[$i]['sendshui'] += $gbao[$j][$i]['sendshui'];
            $zbao[$i]['sendzhong'] += $gbao[$j][$i]['sendzhong'];
            $zbao[$i]['upje']     = 0;
			$zbao[$i]['zje']     = 0;
            $zbao[$i]['shui']     = 0;
            $zbao[$i]['zhong']    = 0;
            $zbao[$i]['yk']       = 0;
            $zbao[$i]['sendyk']   = 0;
            $zbao[$i]['fly']      = 2;
            $zbao[$i]['ifagent']  = $gbao[$j][$i]['ifagent'];
            $zbao[$i]['userid']   = $uid;
            $zbao[$i]['username'] = $gbao[$j][$i]['username'];
        }
        $end = microtime();
        sort($bao);
        $bao = array(
            "gbao" => $gbao,
            "zbao" => $zbao,
			"plc" => $plc
        );
        echo json_encode($bao);
        unset($bao);
        unset($zbao);
        unset($gbao);
        break;
    case "getbao":
	    $jsstatus   = $_POST['jsstatus'];
        $bid   = $_POST['bid'];
        $cid   = $_POST['cid'];
        $sid   = $_POST['sid'];
        $start = rdates($_POST['start']);
        $end   = rdates($_POST['end']);
        $uid   = $_POST['uid'];
        $game  = $_POST['game'];
        $game  = explode('|', $game);
        array_pop($game);
        
        $gstr  = '(' . implode(',', $game) . ')';
        //$start = strtotime($start . ' ' . $config['editend']);
        //$end   = strtotime($end . ' ' . $config['editstart']) + 86400;
		//$start =sqltime($start);
		//$end =sqltime($end);
        $whi   = " and dates>='$start' and dates<='$end' ";
        
        $yq  = " and xtype!=2 $whi and bs=1";
        $yq2 = " and xtype=2  $whi and bs=1";
        if (is_numeric($bid)) {
            $yq .= " and bid='$bid' ";
            $yq2 .= " and bid='$bid' ";
        }
        if (is_numeric($sid)) {
            $yq .= " and sid='$sid' ";
            $yq2 .= " and sid='$sid' ";
        }
        if (is_numeric($cid)) {
            $yq .= " and cid='$cid' ";
            $yq2 .= " and cid='$cid' ";
        }
        $join = " from `$tb_lib`  where  gid in $gstr  ";
        $ustr = 'uid1';
        $bao  = topuser($userid);
        $cb   = count($bao);
		
			$jstr='';
			if($jsstatus==1){
				
				$jstr .= " and z!=9 ";
			}else if($jsstatus==0){
				$jstr .= " and z=9 ";
			}  
	    $yq .= $jstr;
		$yq2 .= $jstr;
        for ($i = 0; $i < $cb; $i++) {
            $whi2 = " and ($ustr='" . $bao[$i]['userid'] . "' or userid='" . $bao[$i]['userid'] . "') $yq ";
            $sql  = "select sum(je),count(id),sum(points*je/100) $join $whi2  ";
            $msql->query($sql);
            $msql->next_record();
            $bao[$i]['userzs']     = pr0($msql->f(1));
            $bao[$i]['userje']     = pr2($msql->f(0));
            $bao[$i]['userpoints'] = pr2($msql->f(2));
            if ($bao[$i]['userzs'] == 0) {
                unset($bao[$i]);
                continue;
            }
            $sql = "select sum(je*zc0/100),count(id),sum(if($ustr=0,points,points1)*je*zc0/(100*100)) $join and z!=2 and z!=7   $whi2";
            $msql->query($sql);
            $msql->next_record();
            $bao[$i]['zs']     = pr0($msql->f(1));
            $bao[$i]['upje']   = pr2($msql->f(0));
            $bao[$i]['points'] = pr2($msql->f(2));
            
            
            $sql = "select sum(if($ustr=0,peilv1,peilv11)*je*zc0/(100))  $join and z=1 $whi2 ";
            $msql->query($sql);
            $msql->next_record();
            $bao[$i]['zhong'] = pr2($msql->f(0));
            $sql              = "select sum(if($ustr=0,peilv2,peilv21)*je*zc0/(100)) $join and z=3 $whi2";
            $msql->query($sql);
            $msql->next_record();
            $bao[$i]['zhong'] += pr2($msql->f(0));
            
            $sql = "select sum(prize*zc0/100) $join and gid=100 and  z=5 $whi2 ";
            $msql->query($sql);
            $msql->next_record();
            $bao[$i]['zhong'] += pr2($msql->f(0));
            
            $bao[$i]['yk'] += p2($bao[$i]['upje'] - $bao[$i]['points'] - $bao[$i]['zhong']);
        }
        $i++;
        $bao[$i]['username'] = "集团外补";
        $bao[$i]['userid']   = $userid;
        $bao[$i]['ifagent']  = 0;
        $bao[$i]['layer']    = 0;
        $bao[$i]['ttype']    = 2;
        $whi2                = " and userid='$userid' $yq2 ";
        $join                = " from `$tb_lib`  where   gid in $gstr  ";
        $sql                 = "select sum(je),count(id),sum(points*je/100) $join and z!=2 and z!=7   $whi2  ";
        $msql->query($sql);
        $msql->next_record();
        $bao[$i]['userzs']     = pr0($msql->f(1));
        $bao[$i]['userje']     = 0;
        $bao[$i]['userpoints'] = 0;
        $bao[$i]['zs']         = pr0($msql->f(1));
        $bao[$i]['upje']       = 0 - pr2($msql->f(0));
        $bao[$i]['points']     = 0 - pr2($msql->f(2));
        if ($bao[$i]['zs'] == 0) {
            unset($bao[$i]);
        } else {
            $sql = "select sum(if($ustr=0,peilv1,peilv11)*je*zc0/(100))  $join  and z=1 $whi2";
            $msql->query($sql);
            $msql->next_record();
            $bao[$i]['zhong'] = pr2($msql->f(0));
            $sql              = "select sum(if($ustr=0,peilv2,peilv21)*je*zc0/(100)) $join and z=3 $whi2";
            $msql->query($sql);
            $msql->next_record();
            $bao[$i]['zhong'] += pr2($msql->f(0));
            $bao[$i]['yk'] += p2(0 - $bao[$i]['points'] + $bao[$i]['zhong'] + $bao[$i]['upje']);
        }
        
        sort($bao);
        echo json_encode($bao);
        unset($bao);
        break;
    case "baoagent":
	   //error_reporting(E_ALL);
	    $jsstatus   = $_POST['jsstatus'];
        $bid   = $_POST['bid'];
        $cid   = $_POST['cid'];
        $sid   = $_POST['sid'];
        $start = rdates($_POST['start']);
        $end   = rdates($_POST['end']);
        $uid   = $_POST['uid'];
        $game  = $_POST['game'];
        $game  = explode('|', $game);
        array_pop($game);
        
        $gstr  = '(' . implode(',', $game) . ')';
        //$start = strtotime($start . ' ' . $config['editend']);
        //$end   = strtotime($end . ' ' . $config['editstart']) + 86400;
		//$start =sqltime($start);
		//$end =sqltime($end);
        $whi   = " and dates>='$start' and dates<='$end' ";
        
        $yq  = " and xtype!=2 $whi and bs=1";
        $yq2 = " and xtype=2  $whi and bs=1";
        if (is_numeric($bid)) {
            $yq .= " and bid='$bid' ";
            $yq2 .= " and bid='$bid' ";
        }
        if (is_numeric($sid)) {
            $yq .= " and sid='$sid' ";
            $yq2 .= " and sid='$sid' ";
        }
        if (is_numeric($cid)) {
            $yq .= " and cid='$cid' ";
            $yq2 .= " and cid='$cid' ";
        }
        $join = " from `$tb_lib`  where gid in $gstr ";
        $bao  = topuser($uid);
        $cb   = count($bao);
        if ($cb == 0) {
            //$bao = array();
            //echo json_encode($bao);
            //exit;
        }

			$jstr='';
			if($jsstatus==1){
				
				$jstr .= " and z!=9 ";
			}else if($jsstatus==0){
				$jstr .= " and z=9 ";
			}  
			
	    $yq .= $jstr;
		$yq2 .= $jstr;
		
		$msql->query("select layer,plc from `$tb_user` where userid='$uid'");
		$msql->next_record();
		$layer       = $msql->f('layer');
		$plc       = $msql->f('plc');
        $myid        = 'uid' . $layer;
        $myzcstr     = 'zc' . $layer;
        $mypointsstr = 'points' . $layer;
        $mypeilv1str = 'peilv1' . $layer;
        $mypeilv2str = 'peilv2' . $layer;
		$i=0;
        for (;$i < $cb; $i++) {
            if ($layer < 8) {
                $uidstrdown    = 'uid' . ($layer + 1);
                $pointsstrdown = 'points' . ($layer + 1);
                $peilv1strdown = 'peilv1' . ($layer + 1);
                $peilv2strdown = 'peilv2' . ($layer + 1);
            } else {
                $uidstrdown    = 'userid';
                $pointsstrdown = 'points';
                $peilv1strdown = 'peilv1';
                $peilv2strdown = 'peilv2';
            }
            $zcstrdown = '';
            for ($k = 8; $k >= $bao[$i]['layer']; $k--) {
                $zcstrdown .= '-zc' . $k;
            }
            $zcstrup = $zcstrdown . '-zc' . $k;
            if ($bao[$i]['ifagent'] == '0') {
				
                $msql->query("select sum(je),sum(je*points/100),count(id) $join and userid='" . $bao[$i]['userid'] . "'  and z!=2 and z!=7  $yq");
                $msql->next_record();
                $bao[$i]['upje'] = pr2($msql->f(0));
				$bao[$i]['zje'] = pr2($msql->f(0));
                $bao[$i]['shui'] = pr2($msql->f(1));
                if ($bao[$i]['upje'] == 0) {
                    unset($bao[$i]);
                    continue;
                }
                $bao[$i]['zs'] = pr0($msql->f(2));
                $msql->query("select sum(peilv1*je) $join and userid='" . $bao[$i]['userid'] . "'  and z=1 $yq");
                $msql->next_record();
                $bao[$i]['zhong'] = pr2($msql->f(0));
                $msql->query("select sum(peilv2*je) $join and userid='" . $bao[$i]['userid'] . "'  and z=3 $yq");
                $msql->next_record();
                $bao[$i]['zhong'] += pr2($msql->f(0));
                
                $sql = "select sum(prize) $join  and userid='" . $bao[$i]['userid'] . "' and gid=100 and z=5   $yq ";
                $msql->query($sql);
                $msql->next_record();
                $bao[$i]['zhong'] += pr2($msql->f(0));
                $bao[$i]['yk']    = pr2($bao[$i]['upje'] - $bao[$i]['shui'] - $bao[$i]['zhong']);
                $bao[$i]['ttype'] = 0;
            } else {
                $sql = "select sum((100" . $zcstrdown . ")*je/100),count(id),sum(if($uidstrdown=0,(points*je/100),$pointsstrdown*je*(100 $zcstrdown)/(100*100))),sum(je) $join and ($uidstrdown='" . $bao[$i]['userid'] . "' or userid='" . $bao[$i]['userid'] . "')  and z!=2 and z!=7  $yq";
                $msql->query($sql);
                $msql->next_record();
                $bao[$i]['zs']   = pr0($msql->f(1));
                $bao[$i]['upje'] = pr2($msql->f(0));
				$bao[$i]['zje'] = pr2($msql->f(3));
                if ($bao[$i]['zs'] == 0) {
                    
                    unset($bao[$i]);
                    continue;
                }
                $bao[$i]['shui'] = pr2($msql->f(2));
                $sql             = "select sum(if($uidstrdown=0,(peilv1*je),$peilv1strdown*(100 $zcstrdown)*je/100)) $join and ($uidstrdown='" . $bao[$i]['userid'] . "' or userid='" . $bao[$i]['userid'] . "') and z=1 $yq";
                $msql->query($sql);
                $msql->next_record();
                $bao[$i]['zhong'] = pr2($msql->f(0));
                $sql              = "select sum(if($uidstrdown=0,(peilv2*je),$peilv2strdown*(100 $zcstrdown)*je/100)) $join and ($uidstrdown='" . $bao[$i]['userid'] . "' or userid='" . $bao[$i]['userid'] . "')  and z=3 $yq";
                $msql->query($sql);
                $msql->next_record();
                $bao[$i]['zhong'] += pr2($msql->f(0));
                
                $sql = "select sum(prize*(100 $zcstrdown)/100) $join and ($uidstrdown='" . $bao[$i]['userid'] . "' or userid='" . $bao[$i]['userid'] . "') and gid=100 and z=5 $yq";
                $msql->query($sql);
                $msql->next_record();
                $bao[$i]['zhong'] += pr2($msql->f(0));
                $bao[$i]['yk'] = pr2($bao[$i]['upje'] - $bao[$i]['shui'] - $bao[$i]['zhong']);
            }
            $sql = "select sum($myzcstr*je/100),sum(if($uidstrdown=0,(points*$myzcstr*je/(100*100)),$pointsstrdown*$myzcstr*je/(100*100)))  $join and ($uidstrdown='" . $bao[$i]['userid'] . "' or userid='" . $bao[$i]['userid'] . "')  and z!=2 and z!=7  $yq";
            $msql->query($sql);
            $msql->next_record();
            $bao[$i]['mezc']   = pr2($msql->f(0));
            $bao[$i]['meshui'] = pr2($msql->f(1));
            $sql               = "select sum(if($uidstrdown=0,(peilv1*$myzcstr)*je/100,$peilv1strdown*$myzcstr*je/100))   $join and ($uidstrdown='" . $bao[$i]['userid'] . "' or userid='" . $bao[$i]['userid'] . "') and z=1 $yq";
            $msql->query($sql);
            $msql->next_record();
            $bao[$i]['mezhong'] = pr2($msql->f(0));
            $sql                = "select sum(if($uidstrdown=0,(peilv2*$myzcstr)*je/100,$peilv2strdown*$myzcstr*je/100))  $join and ($uidstrdown='" . $bao[$i]['userid'] . "' or userid='" . $bao[$i]['userid'] . "') and z=3 $yq";
            $msql->query($sql);
            $msql->next_record();
            $bao[$i]['mezhong'] += pr2($msql->f(0));
            $sql = "select sum(prize*$myzcstr/100) $join  and ($uidstrdown='" . $bao[$i]['userid'] . "' or userid='" . $bao[$i]['userid'] . "') and gid=100 and z=5 $yq ";
            $msql->query($sql);
            $msql->next_record();
            $bao[$i]['mezhong'] += pr2($msql->f(0));
            
            $bao[$i]['meyk'] = pr2($bao[$i]['mezc'] - $bao[$i]['meshui'] - $bao[$i]['mezhong']);
            $sql             = "select sum((100 $zcstrup)*je/100),sum($mypointsstr*(100 $zcstrup)*je/(100*100))  $join and ($uidstrdown='" . $bao[$i]['userid'] . "' or userid='" . $bao[$i]['userid'] . "' )  and z!=2 and z!=7  $yq ";
            $msql->query($sql);
            $msql->next_record();
            $bao[$i]['sendje']   = pr2($msql->f(0));
            $bao[$i]['sendshui'] = pr2($msql->f(1));
            
            $sql = "select sum((100 $zcstrup)*je*$mypeilv1str/100)  $join and ($uidstrdown='" . $bao[$i]['userid'] . "' or userid='" . $bao[$i]['userid'] . "' ) and z=1 $yq ";
            $msql->query($sql);
            $msql->next_record();
            $bao[$i]['sendzhong'] = pr2($msql->f(0));
            $sql                  = "select sum((100 $zcstrup)*je*$mypeilv2str/100) $join and ($uidstrdown='" . $bao[$i]['userid'] . "' or userid='" . $bao[$i]['userid'] . "') and z=3 $yq ";
            $msql->query($sql);
            $msql->next_record();
            $bao[$i]['sendzhong'] += pr2($msql->f(0));
            $sql = "select sum(prize*(100 $zcstrup)/100)  ";
            $sql .= "  $join and ($uidstrdown='" . $bao[$i]['userid'] . "' or userid='" . $bao[$i]['userid'] . "') and gid=100 and z=5 $yq ";
            $msql->query($sql);
            $msql->next_record();
            $bao[$i]['sendzhong'] += pr2($msql->f(0));
            
            $bao[$i]['sendyk'] = pr2($bao[$i]['sendshui'] + $bao[$i]['sendzhong'] - $bao[$i]['sendje']);
        }
        $bao[$i]['username'] = "fly1";
        $bao[$i]['fly']      = 1;
        $bao[$i]['userid']   = $uid;
		if(transuser($uid,'fudong')==1){
           $bao[$i]['username'] = transu($uid) . "-自投";
		}else{
           $bao[$i]['username'] = transu($uid) . "-内补";
		}
        $msql->query("select sum(je),count(id),sum(je*points/100) $join and userid='$uid' and z!=2 and z!=7  $yq  ");
        $msql->next_record();
        $bao[$i]['zs']     = pr0($msql->f(1));
        $bao[$i]['mezc']   = pr2($msql->f(0));
        $bao[$i]['meshui'] = pr2($msql->f(2));
        $msql->query("select sum(peilv1*je) $join and userid='$uid' and z=1 $yq");
        $msql->next_record();
        $bao[$i]['mezhong'] = pr2($msql->f(0));
        $msql->query("select sum(peilv2*je) $join and userid='$uid' and z=3 $yq ");
        $msql->next_record();
        $bao[$i]['mezhong'] += pr2($msql->f(0));
        $msql->query("select sum(prize) $join and userid='$uid' and gid=100 and z=5 $yq ");
        $msql->next_record();
        $bao[$i]['mezhong'] += pr2($msql->f(0));
        $bao[$i]['meyk']      = pr2($bao[$i]['meshui'] + $bao[$i]['mezhong'] - $bao[$i]['mezc']);
        $bao[$i]['sendje']    = $bao[$i]['mezc'];
        $bao[$i]['sendshui']  = $bao[$i]['meshui'];
        $bao[$i]['sendzhong'] = $bao[$i]['mezhong'];
        $bao[$i]['sendyk']    = pr2($bao[$i]['meshui'] + $bao[$i]['mezhong'] - $bao[$i]['mezc']);
        $bao[$i]['mezc']      = 0 - $bao[$i]['mezc'];
        $bao[$i]['meshui']    = 0 - $bao[$i]['meshui'];
        $bao[$i]['mezhong']   = 0 - $bao[$i]['mezhong'];
        $bao[$i]['upje']      = 0;
		$bao[$i]['zje']      = 0;
        $bao[$i]['shui']      = 0;
        $bao[$i]['zhong']     = 0;
        $bao[$i]['yk']        = 0;
        $bao[$i]['ttype']     = 1;
        $bao[$i]['ifagent']   = 0;
        $i++;
        $bao[$i]['username'] = "fly1";
        $bao[$i]['fly']      = 2;
        $bao[$i]['userid']   = $uid;
        $bao[$i]['username'] = transu($uid) . "-外补";
        $join                = " from `$tb_lib`  where 1=1 $yq2 $whi ";
        $msql->query("select sum(je),count(id),sum(je*points/100) $join and userid='$uid' and z!=2 and z!=7  $yq2 ");
        $msql->next_record();
        $bao[$i]['zs']     = pr0($msql->f(1));
        $bao[$i]['mezc']   = pr2($msql->f(0));
        $bao[$i]['meshui'] = pr2($msql->f(2));
        $msql->query("select sum(peilv1*je) $join and userid='$uid' and z=1 $yq2 ");
        $msql->next_record();
        $bao[$i]['mezhong'] = pr2($msql->f(0));
        $msql->query("select sum(peilv2*je) $join and userid='$uid' and z=3 $yq2 ");
        $msql->next_record();
        $bao[$i]['mezhong'] += pr2($msql->f(0));
        $bao[$i]['meyk']      = pr2($bao[$i]['meshui'] + $bao[$i]['mezhong'] - $bao[$i]['mezc']);
        $bao[$i]['sendje']    = 0;
        $bao[$i]['sendshui']  = 0;
        $bao[$i]['sendzhong'] = 0;
        $bao[$i]['mezc']      = 0 - $bao[$i]['mezc'];
        $bao[$i]['meshui']    = 0 - $bao[$i]['meshui'];
        $bao[$i]['mezhong']   = 0 - $bao[$i]['mezhong'];
        $bao[$i]['upje']      = 0;
		$bao[$i]['zje']      = 0;
        $bao[$i]['shui']      = 0;
        $bao[$i]['zhong']     = 0;
        $bao[$i]['yk']        = 0;
        $bao[$i]['sendyk']    = 0;
        $bao[$i]['ttype']     = 2;
        $bao[$i]['ifagent']   = 0;
        
        sort($bao);
		//print_r($bao);
		$bao = array("bao" => $bao,"plc" => $plc);
        echo json_encode($bao);
        unset($bao);
        break;
    case "baouser":
        $jsstatus   = $_POST['jsstatus'];
        $ttype = $_POST['ttype'];
        $bid   = $_POST['bid'];
        $cid   = $_POST['cid'];
        $sid   = $_POST['sid'];
        $start = rdates($_POST['start']);
        $end   = rdates($_POST['end']);
        $uid   = $_POST['uid'];
        $game  = $_POST['game'];
        
  
        
        $game = explode('|', $game);
        array_pop($game);
        $gstr  = '(' . implode(',', $game) . ')';
        //$start = strtotime($start . ' ' . $config['editend']);
        //$end   = strtotime($end . ' ' . $config['editstart']) + 86400;
		//$start =sqltime($start);
		//$end =sqltime($end);
        $whi   = " and dates>='$start' and dates<='$end' ";
        
        if ($ttype == 2) {
            $yq = " and xtype=2 $whi and bs=1";
        } else {
            $yq = " and xtype!=2 $whi and bs=1";
        }
        if (is_numeric($bid)) {
            $yq .= " and bid='$bid' ";
        }
        if (is_numeric($sid)) {
            $yq .= " and sid='$sid' ";
        }
        if (is_numeric($cid)) {
            $yq .= " and cid='$cid' ";
        }
			$jstr='';
			if($jsstatus==1){
				
				$jstr .= " and z!=9 ";
			}else if($jsstatus==0){
				$jstr .= " and z=9 ";
			}  
			 
	    $yq .= $jstr;	
		
        $join  = " from `$tb_lib`  where gid in $gstr ";
        $page  = $_POST['page'];
        $psize = $config['psize3'];
        $msql->query("select count(id) $join and userid='$uid' $yq ");
        $msql->next_record();
        $rcount = pr0($msql->f(0));
        $pcount = $rcount % $psize == 0 ? $rcount / $psize : ($rcount - ($rcount % $psize)) / $psize + 1;
        if (!is_numeric($page) | $page < 1 | $page > $pcount)
            $page = 1;
        $msql->query("select * $join and userid='$uid'   $yq order by gid,time desc,id desc limit " . ($page - 1) * $psize . "," . $psize);
        $tz     = array();
        $i      = 0;
        $je     = 0;
        $points = 0;
        $res    = 0;
        
        $tmp = array();
        while ($msql->next_record()) {

            if ($gid != $msql->f('gid') & $i > 0) {
                $tz[$i]['je']     = $je;
                $tz[$i]['points'] = $points;
                $tz[$i]['res']    = $res;
                $je               = 0;
                $res              = 0;
                $points           = 0;
                $i++;
            }
            $tz[$i]['xtype'] = transxtype($msql->f('xtype'));
            $tz[$i]['tid']   = $msql->f('tid');
            $tz[$i]['time']  = substr($msql->f('time'),5);
            
            if ($tmp['g' . $msql->f('gid')] == '') {
                $fsql->query("select gname,mnum,class,xsort from `$tb_game` where gid='" . $msql->f('gid') . "'");
                $fsql->next_record();
                $tmp['g' . $msql->f('gid')]  = $fsql->f('gname');
                $tmp['gc' . $msql->f('gid')] = $fsql->f('class');
             
                for ($j = 1; $j <= $fsql->f('mnum'); $j++) {
                    if ($j > 1)
                        $tmp['gms' . $msql->f('gid')] .= ",";
                    $tmp['gms' . $msql->f('gid')] .= "m" . $j;
                }
            }
         
            $tz[$i]['gid']   = $tmp['g' . $msql->f('gid')];
            $tz[$i]['style'] = $tmp['gc' . $msql->f('gid')];
            $tz[$i]['gids']  = $msql->f('gid');
            
            if ($tmp['b' . $msql->f('gid') . $msql->f('bid')] == '') {
                $tmp['b' . $msql->f('gid') . $msql->f('bid')] = transb8('name', $msql->f('bid'), $msql->f('gid'));
            }
            if ($tmp['s' . $msql->f('gid') . $msql->f('sid')] == '') {
                $tmp['s' . $msql->f('gid') . $msql->f('sid')] = transs8('name', $msql->f('sid'), $msql->f('gid'));
            }
            if ($tmp['c' . $msql->f('gid') . $msql->f('cid')] == '') {
                $tmp['c' . $msql->f('gid') . $msql->f('cid')] = transc8('name', $msql->f('cid'), $msql->f('gid'));
            }
            if ($tmp['p' . $msql->f('gid') . $msql->f('pid')] == '') {
                $tmp['p' . $msql->f('gid') . $msql->f('pid')] = transp8('name', $msql->f('pid'), $msql->f('gid'));
            }
            
          $tz[$i]['wf'] = wf($msql->f('gid'),$tmp['b' . $msql->f('gid') . $msql->f('bid')],$tmp['s' . $msql->f('gid') . $msql->f('sid')],$tmp['c' . $msql->f('gid') . $msql->f('cid')],$tmp['p' . $msql->f('gid') . $msql->f('pid')]);
            
            $tz[$i]['qishu'] = $msql->f('qishu');
            
            if ($tz[0]['kj']['g' . $msql->f('gid') . $msql->f('qishu')] == '') {
                $rs                                                     = $fsql->arr("select kjtime," . $tmp["gms" . $msql->f('gid')] . " from `$tb_kj` where gid='" . $msql->f('gid') . "' and  qishu='" . $msql->f('qishu') . "' ", 0);
				$kjtime = " @ ".substr($rs[0][0],-8);
				array_splice($rs[0],0,1);
                $tz[0]['kj']['g' . $msql->f('gid') . $msql->f('qishu')] = implode('-',$rs[0]).$kjtime;
            }
            $tz[$i]['user'] = transu($msql->f('userid'));
            $tz[$i]['ab']   = $msql->f('ab');
            $tz[$i]['abcd'] = '@'.$msql->f('abcd');
            if ($msql->f('z') == '3') {
                $tz[$i]['peilv'] = (float) $msql->f('peilv2');
            } else {
                $tz[$i]['peilv'] = (float) $msql->f('peilv1');
            }
            $tz[$i]['points'] = pr2($msql->f('je') * $msql->f('points') / 100);

            $tz[$i]['con']    = $msql->f('content');
            $tz[$i]['je']     = $msql->f('je');
            $tz[$i]['z']      = $msql->f('z');
            if ($msql->f('z') == 1) {
                $tz[$i]['zhong'] = pr2($msql->f('peilv1') * $tz[$i]['je']);
            } else if ($msql->f('z') == 2 | $msql->f('z') == 7) {
                $tz[$i]['zhong']  = $tz[$i]['je'];
                $tz[$i]['points'] = 0;
				$tz[$i]['point'] = 0;
            } else if ($msql->f('z') == 3) {
                $tz[$i]['zhong'] = pr2($msql->f('peilv2') * $tz[$i]['je']);
            }  else if ($msql->f('z') == 5) {
                $tz[$i]['zhong'] = pr2($msql->f('prize'));
            } else {
                $tz[$i]['zhong'] = 0;
            }
            
            $je += $tz[$i]['je'];
            $points += $tz[$i]['points'];
            $gid = $msql->f('gid');
            $i++;
        }
		           
           
        $tz[$i]['je']     = $je;
        $tz[$i]['points'] = $points;
        $tz[$i]['res']    = $res;
        $tzs              = array(
            "tz" => $tz,
            'page' => $pcount
        );
        echo json_encode($tzs);
        unset($tz);
        unset($tzs);
        break;
    
    case "baofly":
	    $jsstatus   = $_POST['jsstatus'];
        $ttype = $_POST['ttype'];
        $bid   = $_POST['bid'];
        $cid   = $_POST['cid'];
        $sid   = $_POST['sid'];
        $start = rdates($_POST['start']);
        $end   = rdates($_POST['end']);
        $uid   = $_POST['uid'];
        $fly   = $_POST['fly'];
        $game  = $_POST['game'];
        $game  = explode('|', $game);
        array_pop($game);
        $gstr  = '(' . implode(',', $game) . ')';
        //$start = strtotime($start . ' ' . $config['editend']);
        //$end   = strtotime($end . ' ' . $config['editstart']) + 86400;
		//$start =sqltime($start);
		//$end =sqltime($end);
        $whi   = " and dates>='$start' and dates<='$end' ";
        if ($fly == 2) {
            $yq = " and xtype=2 and bs=1 $whi";
        } else {
            $yq = " and xtype!=2 and bs=1 $whi";
        }
        if (is_numeric($bid)) {
            $yq .= " and bid='$bid' ";
        }
        if (is_numeric($sid)) {
            $yq .= " and sid='$sid' ";
        }
        if (is_numeric($cid)) {
            $yq .= " and cid='$cid' ";
        }
        $join = " from `$tb_lib` where gid in $gstr  and  userid='$uid'  ";
        $fly  = array();
        $msql->query("select count(id),sum(je),sum(je*points/100) $join and z!=2 and z!=7  $yq ");
        $msql->next_record();
        $fly['zs']     = $msql->f(0);
        $fly['zje']    = pr2($msql->f(1));
        $fly['points'] = pr2($msql->f(2));
        $msql->query("select sum(peilv1*je) $join and z=1 $yq ");
        $msql->next_record();
        $fly['zhong'] = pr2($msql->f(0));
        $msql->query("select sum(peilv2*je) $join and z=3 $yq ");
        $msql->next_record();
        $fly['zhong'] += pr2($msql->f(0));
        if ($fly != 2) {
            $msql->query("select sum(prize) $join and z=5 $yq ");
            $msql->next_record();
            $fly['zhong'] = pr2($msql->f(0));
        }
        echo json_encode($fly);
        unset($fly);
        break;
        
}
?>